<?php

namespace Admin\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;


class ThoikhoabieuTable extends AbstractTableGateway {

    protected $table = 'tkb';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Thoikhoabieu());

        $this->initialize();
    }

    public function fetchAll(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table,array('*'))
				->join(
					'lop',
					'lop.id = tkb.lop',
					array('tenlop'))
				->join(
					'thu',
					'thu.id = tkb.thu',
					array('tenthu'))
				->join(
					'tiet',
					'tiet.id = tkb.tiet',
					array('tentiet'))
				->join(
					'monhoc',
					'monhoc.id = tkb.monhoc',
					array('tenmonhoc'))
				->join(
					'giaovien',
					'giaovien.id = tkb.giaovien',
					array('tengv'));
        $resultSet = $this->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }

    public function getThoikhoabieu($id) {
        $id = (int) $id;
        $rowset = $this->select(array('id' => $id));
        $row = $rowset->current(); 
        return $row;
    }

    public function saveThoikhoabieu(Thoikhoabieu $thoikhoabieu) {
		
			
		
        

        $id = (int) $thoikhoabieu->id;
        if ($id == 0) {
			$data = array(
				'thu' => $thoikhoabieu->thu,
				'monhoc' => $thoikhoabieu->monhoc,
				'tiet' => $thoikhoabieu->tiet,
				'lop' => $thoikhoabieu->lop,
				'giaovien' => $thoikhoabieu->giaovien,
				'namhoc' => $thoikhoabieu->namhoc,
				'hocki' => $thoikhoabieu->hocki,
				'buoi' => $thoikhoabieu->buoi,
			);
            	
                $this->insert($data);
            
        } else {
			
			$data = array(
				'thu' => $thoikhoabieu->thu,
				'monhoc' => $thoikhoabieu->monhoc,
				'tiet' => $thoikhoabieu->tiet,
				'lop' => $thoikhoabieu->lop,
				'giaovien' => $thoikhoabieu->giaovien,
				'namhoc' => $thoikhoabieu->namhoc,
				'hocki' => $thoikhoabieu->hocki,
				'buoi' => $thoikhoabieu->buoi,
			);
            if ($this->getThoikhoabieu($id)) {
				
					$this->update($data, array('id' => $id));
				
            } else {
                throw new \Exception('Không tồn tại');
            }
        }
    }

    public function deleteThoikhoabieu($id) {
        $this->delete(array('id' => $id));
    }
	public function xoaThoikhoabieu($id) {
		
        $this->delete('id IN (' . $id . ')');
    }
	
	//kiem tra ton tai
	public function check_exists($buoi,$thu,$tiet,$giaovien,$namhoc,$hocki,$id)
	{
		//$rowset = $this->select(array('title' => $title));
        //$row = $rowset->current();
		$resultSet = $this->select(array('buoi = "'.$buoi.'"','thu = "'.$thu.'"','tiet = "'.$tiet.'"','giaovien = "'.$giaovien.'"','namhoc = "'.$namhoc.'"','hocki = "'.$hocki.'"', 'id != "'.$id.'"'));
    	$row = $resultSet->current();
        return $row;
		
	}
	


}
